package sz.szsmk.citizencard.retrofitrxjava;

import android.app.Application;

import sz.szsmk.citizencard.db.DaoMaster;
import sz.szsmk.citizencard.db.DaoSession;
import sz.szsmk.citizencard.ioc.dzsmkcompontent.ApplicationComponent;
import sz.szsmk.citizencard.ioc.dzsmkcompontent.DaggerApplicationComponent;
import sz.szsmk.citizencard.ioc.dzsmkmodule.ApplicationModule;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.SecureSharedPreferences;

/**
 * Created by chensf on 17/9/29.
 */

public class DZSmkAppInit {

    private Application mContext;

    //private RefWatcher mRefWatcher;

    private ApplicationComponent mComponent;

    private DaoSession mDaoSession;

    private DZSmkAppInit(){
    }

    public Application getContext() {
        return mContext;
    }



    public void onCreate(Application context) {
        mContext = context;
//        SecureSharedPreferences.putString(Constant.CHANNEL_ID,channelId);
        //mRefWatcher = LeakCanary.install(context);
        initDatabase();
        //CrashReport.initCrashReport(context, "f3f1169c39", true); //测试时true
    }

    private void initDatabase() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mContext, "ec-db", null);
        // 注意：该数据库连接属于 DaoMaster，所以多个 Session 指的是相同的数据库连接。
        DaoMaster daoMaster = new DaoMaster(helper.getWritableDb());
        mDaoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

//    public RefWatcher getRefWatcher() {
//        return mRefWatcher;
//    }

    public ApplicationComponent getComponent() {
        if (null == mComponent) {
            mComponent = DaggerApplicationComponent.builder()
                    .applicationModule(new ApplicationModule(mContext))
                    .build();
        }
        return mComponent;
    }

    public final static DZSmkAppInit getInstance(){
        return DZSmkAppInitInstance.sInstance;
    }



    private static class DZSmkAppInitInstance{
        public static final DZSmkAppInit sInstance = new DZSmkAppInit();
    }
}
